﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model.Entities;
using NHibernate;
using Totoro.ORMapping;

namespace Bussiness.ModelQuery
{
    public class ShopManager
    {
        public ShopManager()
        {
        }
        private static ShopManager instance;
        public static ShopManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ShopManager();
                    instance.LoadData();
                }
                return instance;
            }
        }
        Dictionary<string, ShopsInfo> shopInfoCaches = new Dictionary<string, ShopsInfo>();
        public void LoadData()
        {
            shopInfoCaches.Clear();
            ISession session = NHHelper.GetCurrentSession();
            ICriteria crt = session.CreateCriteria<Products>();
            crt.AddOrder(new NHibernate.Criterion.Order("ShopName", true));//按名称进行排序
            IList<ShopsInfo> shopInfoList = null;
            shopInfoList = crt.List<ShopsInfo>();
            if (shopInfoList != null)
            {
                foreach (ShopsInfo shops in shopInfoList)
                {
                    shopInfoCaches.Add(shops.ID, shops);
                }
            }
        }
        /// <summary>
        /// 根据Id获取商店信息
        /// </summary>
        /// <param name="sId">商店Id</param>
        /// <returns>商店信息</returns>
        public ShopsInfo GetShopInfoById(string sId)
        {
            if (shopInfoCaches.ContainsKey(sId))
            {
                return shopInfoCaches[sId];
            }
            return null;
        }
        /// <summary>
        /// 获取所有的商店信息
        /// </summary>
        /// <returns>商店信息</returns>
        public ShopsInfo[] GetShopInfos()
        {
            return shopInfoCaches.Values.ToArray();
        }
        /// <summary>
        /// 根据商店类型进行分类
        /// </summary>
        /// <param name="sType">商店类型</param>
        /// <returns>商店信息</returns>
        public ShopsInfo[] GetShopInfosByType(int sType)
        {
            List<ShopsInfo> shopInfosList = new List<ShopsInfo>();
            foreach (ShopsInfo shop in shopInfoCaches.Values)
            {
                if (shop.Type == sType)
                {
                    shopInfosList.Add(shop);
                }
            }
            return shopInfosList.ToArray();
        }
        /// <summary>
        /// 根据商店地址进行模糊查询，获取包含字段的商店信息
        /// </summary>
        /// <param name="sAddress">商店地址（不区分大小写）</param>
        /// <returns>商店信息</returns>
        public ShopsInfo[] GetShopInfosByAddress(string sAddress)
        {
            List<ShopsInfo> shopInfosList = new List<ShopsInfo>();
            foreach (ShopsInfo shop in shopInfoCaches.Values)
            {
                if (shop.Address.ToUpper().Contains(sAddress.ToUpper()))
                {
                    shopInfosList.Add(shop);
                }
            }
            return shopInfosList.ToArray();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sId"></param>
        public void DeleteShopByID(string sId)
        {
            ISession session = NHHelper.GetCurrentSession();
            ShopsInfo shop = GetShopInfoById(sId);
            if (shop != null)
            {
                session.Delete(shop);
                shopInfoCaches.Remove(sId);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="shop"></param>
        public void InsertShop(ShopsInfo shop)
        {
            NHHelper.SaveOjb(shop);
            shopInfoCaches.Add(shop.ID, shop);
        }
    }
}
